#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ======================================================
# @File:      : test239
# @Author     : forward_huan
# @Date       : 2024/11/17 10:04
# @Desc       :
# ======================================================
from typing import List


class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        if k >= len(nums):
            return [max(nums)]
        stack, result = [], []
        for i in range(len(nums)):
            while stack and nums[i] > stack[-1]:
                stack.pop()
            stack.append(nums[i])

            if i > (k - 1) and stack[0] == nums[i - k]:
                stack.remove(stack[0])
            if i >= (k - 1):
                result.append(stack[0])

        return result


print(Solution().maxSlidingWindow(nums=[1, 3, -1, -3, 5, 3, 6, 7], k=3))
